package com.smile.frame.seckill.mapper;

import com.smile.frame.seckill.model.SecondKill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
* @author LGC
* @description 针对表【second_kill】的数据库操作Mapper
* @createDate 2025-01-21 10:43:25
* @Entity com.smile.frame.seckill.model.SecondKill
*/
public interface SecondKillMapper extends BaseMapper<SecondKill> {
    /**
     * 将此行数据进行加锁，当整个方法将事务提交后，才会解锁
     * @param skgId
     * @return
     */
    @Select(value = "SELECT * FROM second_kill WHERE id=#{skgId} FOR UPDATE")
    SecondKill querySecondKillForUpdate(@Param("skgId") Long skgId);

    @Update(value = "UPDATE second_kill SET number=number-1 WHERE id=#{skgId} AND number > 0")
    int updateSecondKillById(@Param("skgId") long skgId);

    @Update(value = "UPDATE second_kill  SET number=number-#{number},version=version+1 WHERE id=#{skgId} AND version = #{version}")
    int updateSecondKillByVersion(@Param("number") int number, @Param("skgId") long skgId, @Param("version")int version);

}




